package bg.cinema_system.beans;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.sql.DataSource;

import bg.cinema_system.DAO.UserInfoDAO;
import bg.cinema_system.DAO.UserTicketsDAO;
import bg.cinema_system.models.UserTickets;

public class UserTicketsListBean {

	// Resource Injection
	@Resource(name = "jdbc/cinema")
	private DataSource dataSource;

	private long user_id;
	private String username;
	private List<UserTickets> userTickets;

	public long getUser_id() {
		return user_id;
	}

	public void setUser_id(long user_id) {
		this.user_id = user_id;
	}

	public String getUsername() throws SQLException {
		setUser_id();
		getUserTicketsList();

		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<UserTickets> getUserTickets() {
		return userTickets;
	}

	public void setUserTickets(List<UserTickets> userTickets) {
		this.userTickets = userTickets;
	}

	// Set the "movie id", using the passed parameter
	public void setUser_id() {
		String user_id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("user_id");

		this.user_id = Long.parseLong(user_id);
	}

	// Set all fields of the MovieBean, using the JDBC connection to the
	// database using the "movie id"
	public void getUserTicketsList() throws SQLException {
		UserTicketsDAO userTicketsDAO = new UserTicketsDAO(this.user_id,
				dataSource);
		UserInfoDAO userInfoDAO = new UserInfoDAO(this.user_id, dataSource);

		this.userTickets = userTicketsDAO.getUserTicketsList();
		this.username = userInfoDAO.getUsername();
	}

}
